/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Network;
import com.tesam.gedeon.entity.Provider;
import com.tesam.gedeon.entity.Simcard;
import com.tesam.gedeon.service.NetworkService;
import com.tesam.gedeon.service.ProviderService;
import com.tesam.gedeon.service.SimcardService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
@Controller
public class SimcardController {
    
    @Autowired
    SimcardService  simcardService;
    @Autowired
    SystemUserController system;
    @Autowired
    ProviderService providerService;
    @Autowired
    NetworkService networkService;
    Network network = new Network();
    Provider provider = new Provider();
    
    @RequestMapping(value ="/mediationsimcard/new")
    public ModelAndView newSimcard(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/new");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("provider", providerService.getAll());
        mv.addObject("network", networkService.getListAll());
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationsimcard/delete")
    public ModelAndView deleteSimcard(@ModelAttribute("Simcard") Simcard simcard ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/delete");
            simcardService.delete(simcard);
        return mv;
    }
    
    @RequestMapping(value = "/mediationsimcard/saveupdate")
    public ModelAndView updateSimcard(@ModelAttribute("Simcard") Simcard simcard ,HttpServletRequest request){
    ModelAndView mv = new ModelAndView("/mediationsimcard/save");
    provider.setId(Integer.parseInt(request.getParameter("CboProvider").toString()));
        network.setId(Integer.parseInt(request.getParameter("CboNetwork").toString()));
        simcard.setProvider(provider);
        simcard.setNetwork(network);
    simcardService.update(simcard);
    mv.addObject("id", simcard.getId());
    
   return mv; 
    }
    
    @RequestMapping(value = "/mediationsimcard/searchByIccid")
    public ModelAndView searchByIccid(@ModelAttribute("Simcard") Simcard simcard ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/searchByIccid");
       
        mv.addObject("count", simcardService.getCountByIccid(simcard));
        
        return mv;
    }
    
    @RequestMapping(value ="/mediationsimcard/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/index");
        
        system.loadDataSystem(mv, request);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationsimcard/newsave")
    public ModelAndView saveSimcard(@ModelAttribute("Simcard") Simcard simcard ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/save");
        System.out.println("entro : ");
        provider.setId(Integer.parseInt(request.getParameter("CboProvider").toString()));
        network.setId(Integer.parseInt(request.getParameter("CboNetwork").toString()));
        simcard.setProvider(provider);
        simcard.setNetwork(network);
        mv.addObject("id", simcardService.save(simcard));
        return mv;
    }
    
    @RequestMapping(value = "/mediationsimcard/update")
    public ModelAndView update(@ModelAttribute("Simcard") Simcard simcard , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/update");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("simcard", simcardService.getSimcardById(simcard));
        mv.addObject("provider", providerService.getAll());
        mv.addObject("network", networkService.getListAll());
        
        return mv;
    }
    
    @RequestMapping(value ="/mediationsimcard/view")
    public ModelAndView view(@ModelAttribute("Simcard") Simcard simcard , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationsimcard/view");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("simcard", simcardService.getSimcardById(simcard));
        
        return mv;
    }
    
      @RequestMapping(value = "/mediationsimcard/ajax" , method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int quantityrecord = 0;
        try {
            List<Simcard> list = simcardService.getSimcardByLimit(0, 20, "si.iccid", "asc");

            // ArrayList<Cliente> list = clienteService.getAllClientes();
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (Simcard simcard : list) {
                    JSONArray arrayPut = new JSONArray(); 
                    arrayPut.put(simcard.getIccid()); 
                    arrayPut.put(simcard.getImsi()); 
                    arrayPut.put(simcard.getMsisdn());
                    arrayPut.put(simcard.getType());
                    arrayPut.put(simcard.getMovilName());
                    arrayPut.put(simcard.getPlantype());
                    arrayPut.put(simcard.getProvider().getName());
                    arrayPut.put(simcard.getNetwork().getDescription());
                    array.put(arrayPut); 
                }
            }


            quantityrecord = simcardService.getCountAll();
            json.put("iTotalRecords", quantityrecord);
            json.put("iTotalDisplayRecords", quantityrecord);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Controller-doAjax:" + e.getMessage());
        }

        return null;
    }
     
      @RequestMapping(value = "/mediationsimcard/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<Simcard> listSimcard = null;
        int quantityrecord = 0; 
                                                    
        String[] columns = {"si.iccid", "si.imsi", "si.msisdn" , "si.type" , "si.movilename" , "si.plantype" , "si.imsi", "si.imsi", "si.imsi"};

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 0;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                Simcard Simcard = new Simcard();
                Simcard.setIccid(textSearch);
                listSimcard = simcardService.getSimcardBySearch(_startLimit,_endLimit,dataToSort , directionOfOrder , Simcard);
                quantityrecord = simcardService.getCountBySearch(Simcard);
                return simcardService.getJsonDataFromArrayList(listSimcard, quantityrecord);

            }
        }

        listSimcard = simcardService.getSimcardByLimit(_startLimit,
                (_endLimit), dataToSort, directionOfOrder);

        quantityrecord = simcardService.getCountAll();

        return simcardService.getJsonDataFromArrayList(listSimcard, quantityrecord);
    }

    
}
